package club.calong.sanniu.controller;

import club.calong.sanniu.http.response.Response;
import club.calong.sanniu.pojo.Video;
import club.calong.sanniu.service.VideoService;
import club.calong.sanniu.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video")
public class VideoController {

    @Value("${app.host}")
    String APP_HOST;

    @Value("${app.video.path}")
    String VIDEO_PATH;

    VideoService service;

    @Autowired
    public VideoController(VideoService service) {
        this.service = service;
    }

    @PostMapping
    public Response save (@RequestParam("video") MultipartFile file, @RequestParam("name") String name) {
        checkResourcesDirExits();
        System.out.println( file.getOriginalFilename());
        if (file != null && file.getOriginalFilename() != null) {
            File video;
            String fileName;
           
            String[] originalName = file.getOriginalFilename().split("\\.");
            String suffix = originalName[originalName.length - 1];
            fileName =  new File(VIDEO_PATH).getAbsolutePath() +"/" +  System.currentTimeMillis() + suffix;
        
            video = new File(fileName);
            System.out.println("real path is :" + video.getAbsolutePath());
            try {
                System.out.println("related path is :" + fileName);
                System.out.println("real path is :" + video.getAbsolutePath());
                file.transferTo(video);
                String url = APP_HOST + "res/video/" + fileName.substring(fileName.lastIndexOf("/") + 1);

                Video videobean = new Video(name, url);
                videobean.setSize(file.getSize());
                if (service.save(videobean)) {
                    return new Response(Response.SUCCESS, Response.Message.UPLOAD_OK, video);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return new Response(Response.FAILURE, Response.Message.UPLOAD_ERROR, null);
    }

    @GetMapping("list/{page}/{size}")
    public Response page (@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

        return new Response(Response.SUCCESS, null, service.page(page, size));
    }

    @GetMapping("list")
    public Response page () {

        return new Response(Response.SUCCESS, null, service.page(null, null));
    }

    @DeleteMapping("{id}")
    public Response delete (@PathVariable("id") Integer id) {

        if (service.delete(id)) {
            return new Response(Response.SUCCESS, Response.Message.DELETE_OK, null);
        } else {
            return new Response(Response.FAILURE, Response.Message.DELETE_ERROR, null);
        }
    }
    public void checkResourcesDirExits () {
        File   file=new   File(".");
        String path=file.getAbsolutePath();
        System.out.println("current dir is :" + path);
        File publichdir = new File("./public");
        File videodir = new File("./public/video");
        File imagedir = new File("./public/image");
        if(!(publichdir.exists() && publichdir.isDirectory())){
            publichdir.mkdir();
        }
        if(!(videodir.exists() && videodir.isDirectory())){
            videodir.mkdir();
        }
        if(!(imagedir.exists() && imagedir.isDirectory())){
            imagedir.mkdir();
        }
        path=file.getPath();
    }
}
